package com.lxujia.ruankao.fragment;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lxujia.ruankao.R;
import com.lxujia.ruankao.util.ConstantUtil;
import com.lxujia.ruankao.util.LogUtil;
import com.lxujia.ruankao.util.AlipayUtil;

import mehdi.sakout.aboutpage.AboutPage;
import mehdi.sakout.aboutpage.Element;

import com.baidu.autoupdatesdk.BDAutoUpdateSDK;
import com.baidu.autoupdatesdk.UICheckUpdateCallback;

public class AboutFragment extends android.support.v4.app.Fragment {
    private final static String TAG = "AboutFragment";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = new AboutPage(getContext())
                .isRTL(false)
                .setImage(R.mipmap.ic_launcher)
                .setDescription(getString(R.string.about_description))
                .addItem(getCheckUpdate())
                .addItem(getQQElement())
                .addItem(getRewardElement())
                .create();

        return view;
    }

    Element getCheckUpdate() {
        Element upDateElement = new Element();
        upDateElement.setTitle(getString(R.string.check_update) + "（版本：" + getVersionName() + "）");
        upDateElement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BDAutoUpdateSDK.uiUpdateAction(getActivity(), new MyUICheckUpdateCallback());
            }
        });
        return upDateElement;
    }

    Element getRewardElement() {
        Element rewardElement = new Element();
        final String rewardTitle = String.format(getString(R.string.reward_authors));
        rewardElement.setTitle(rewardTitle);
        rewardElement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转支付宝收款
                try {
                    AlipayUtil.startAlipayClient(getActivity());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return rewardElement;
    }

    Element getQQElement() {
        Element qqElement = new Element();
        final String qqTitle = String.format(getString(R.string.connect_with_qqgroup));
        qqElement.setTitle(qqTitle);
        qqElement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转QQ群界面
                joinQQGroup(ConstantUtil.QQGROUP_KEY);
            }
        });
        return qqElement;
    }

    public void joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse(
                "mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D"
                        + key));
        try {
            startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(getContext(), "您还没有安装QQ，请先安装软件", Toast.LENGTH_LONG).show();
        }
    }

    private String getVersionName() {
        String localVersion = "";
        try {
            PackageInfo packageInfo = getContext().getPackageManager()
                    .getPackageInfo(getContext().getPackageName(), 0);
            localVersion = packageInfo.versionName;
            LogUtil.d("TAG", "本软件的版本号。。" + localVersion);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }

    private class MyUICheckUpdateCallback implements UICheckUpdateCallback {

        @Override
        public void onNoUpdateFound() {
            LogUtil.d(TAG, "onNoUpdateFound");
            Toast.makeText(getActivity(), R.string.no_update_found, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCheckComplete() {
            LogUtil.d(TAG, "onNoUpdateFound");
        }
    }
}
